/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import preprocessing.UnsupervisedDiscritize;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import weka.filters.unsupervised.attribute.NumericToNominal;

/**
 *
 * @author Winzelric
 */
public class WK {

    public Instances data;
    public ArrayList<ArrayList<String>> strukdat = new ArrayList<ArrayList<String>>();
    public String nameoffile = "";
    /**
     * @param args the command line arguments
     */
    public void print(Instances data) {

        for (int i = 0; i < data.numInstances(); i++) {
            for (int j = 0; j < data.numAttributes(); j++) {
                System.out.print(data.instance(i).stringValue(j));
                System.out.print(" ");
            }
            System.out.println("");
        }
    }
    
    public void convert() {
        strukdat.clear();        
        for (int i = 0; i < data.numInstances(); i++) {
            ArrayList<String> temp = new ArrayList<String>();
            for (int j = 0; j < data.numAttributes(); j++) {
                temp.add(data.instance(i).stringValue(j));
            }
            strukdat.add(temp);
        }
    }
    
    public void convertToArrayList () {
        if (strukdat.isEmpty() == false) {
            strukdat.clear();
        }
        try{
            FileInputStream fstream = new FileInputStream(nameoffile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            strLine = br.readLine();
            //Relation = parseRelation(strLine); // Membaca relation
            strLine = br.readLine();
            while (strLine.equals("@data") == false)   {
                strLine = br.readLine();
            }
            strLine = br.readLine();
            ArrayList<String> temp = new ArrayList<String>();
            while (strLine != null) {
                while (strLine.equals("") == false) {
                    String wew = Character.toString(strLine.charAt(0));
                    System.out.println(wew);
                    temp.add(wew);
                    strLine = strLine.substring(1);
                    if (strLine.equals("") == false) {
                        while ((strLine.charAt(0) == ' ') || (strLine.charAt(0) == ',')) {
                            strLine = strLine.substring(1);
                        }
                    }
                }
                strukdat.add(temp);
                temp.clear();
                strLine = br.readLine();
            }
            //Close the input stream
            in.close();
        } catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    public void convertToArff(String csvname, String arffname) throws IOException {
        // load CSV
        CSVLoader loader = new CSVLoader();
        loader.setSource(new File(csvname));
        data = loader.getDataSet();
        UnsupervisedDiscritize wkwk = new UnsupervisedDiscritize(data);
        data = wkwk.returnInstances(data);
        
        // save ARFF
        ArffSaver saver = new ArffSaver();
        saver.setInstances(data);
        System.out.println(arffname);
        saver.setFile(new File(arffname));
        //saver.setDestination(new File(arffname));
        saver.writeBatch();
    }
    
    public void readFromFile(String filename) {
        int mid= filename.lastIndexOf(".");
        String fname=filename.substring(0,mid);
        String ext=filename.substring(mid+1,filename.length());
        if (ext.equals("arff")) {
            BufferedReader reader;
            try {
                reader = new BufferedReader(new FileReader(filename));
                data = new Instances(reader);
                nameoffile = filename;
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
        else if (ext.equals("csv")) {
            try {
                String newfname = fname + ".arff";
                convertToArff(filename, newfname);
                nameoffile = newfname;
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
    
    public void printArray(ArrayList<ArrayList<String>> a) {
        /*int i = 0;
        while (a.get(i) != null) {
            ArrayList<String> temp = a.get(i);
            int it = 0;
            String x="";
            while (temp.get(it).equals("") == false) {
                //x += temp.get(it) + " ";
                System.out.print(temp.get(it));
                System.out.print(" ");
                ++it;
            }
            System.out.println();
            ++i;
        }*/
        System.out.println(a.get(0).get(0));
    }
    
    public void printArr() {
        int i = strukdat.size();
        ArrayList<String> temp = new ArrayList<String>();
        temp = strukdat.get(0);
        int j = temp.size();
        
        for (int x = 0; x < i; x++) {
            temp = strukdat.get(x);
            for (int y = 0; y < j; y++) {
                System.out.print(temp.get(y) + " ");
            }
            System.out.println("");
        }
    }
    
    public void selectAttribute (ArrayList<Integer> select) {
        for (int i = 0; i < select.size(); i++) {
            data.deleteAttributeAt(select.get(i) - i);
        }
        convert();
    }
    
    public Instances returnInsWithAtt (Instances dataawal, ArrayList<Integer> select) {
        for (int i = 0; i < select.size(); i++) {
            dataawal.deleteAttributeAt(select.get(i) - i);
        }
        return dataawal;
    }

    public static void main(String[] args) {
        
        WK b = new WK();
        b.readFromFile("contoh.csv");
        b.convert();
        //b.printArr();
        ArrayList<Integer> haha = new ArrayList<Integer>();
        haha.add(1);
        haha.add(3);
        haha.add(5);
        b.selectAttribute(haha);
        b.print(b.data);
        
        // TODO code application logic here
       /* WK b = new WK();
        BufferedReader reader;

         CSVLoader loader = new CSVLoader(); try { File f = new
         File("contoh.csv"); System.out.println(f.length());
         loader.setSource(f); Instances data = loader.getDataSet();
         
         b.print(data); } catch (IOException ex) {
         System.out.println("HAHAHA"); }

//            reader = new BufferedReader(new FileReader("contoh1.arff"));
        try {
            //Instances data = new Instances(reader);
            b.convertToArff("contoh.csv", "ketoke.arff");
            System.out.println("");
            System.out.println("");
            System.out.println("");
//                UnsupervisedReplaceMissingValue a = new UnsupervisedReplaceMissingValue(data);
//                Instances newdata = a.returnInstances(data);
//                b.print(newdata);
        } catch (IOException ex) {
            //Logger.getLogger(WK.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex);
        }*/
    }
}
